#include "ConsoleStream.h"
#include <conio.h>

namespace Rz { namespace IO {

size_t ConsoleStream::Read(byte_t* buffer, size_t size) const
{
	size_t count = 0;

	while (count < size)
	{
		buffer[count++] = static_cast<byte_t>(_getch());
	}

	return count;
}

String ConsoleStream::ReadLine() const
{
	String result;

	int c;

	while ((c = this->ReadByte()) != '\r')
	{
		const_cast<ConsoleStream*>(this)->WriteByte(static_cast<byte_t>(c));
		result.Append(String((char)c)[0]);
	}

	const_cast<ConsoleStream*>(this)->WriteByte(static_cast<byte_t>('\n'));

	return result;
}

void ConsoleStream::Write(const byte_t* buffer, size_t size)
{
	fwrite(static_cast<const void*>(buffer), 1, size, stdout);
}

void ConsoleStream::WriteLine(const String& str)
{
	this->Write(reinterpret_cast<const byte_t*>(str.GetCharArray()), str.GetLength());
	this->WriteByte('\n');
}


void ConsoleStream::Seek(size_t position)
{
	RZ_UNUSED(position);
}

size_t ConsoleStream::GetPosition() const
{
	return 0;
}

size_t ConsoleStream::GetLength() const
{
	return 0;
}

bool ConsoleStream::IsOpened() const
{
	return true;
}

bool ConsoleStream::CanSeek() const
{
	return false;
}

bool ConsoleStream::CanRead() const
{
	return true;
}

bool ConsoleStream::CanWrite() const
{
	return true;
}


void ConsoleStream::Close()
{
	
}

} }

